package com.doyens.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doyens.common.R;
import com.doyens.dto.RecruiterDTO;
import com.doyens.entity.Level;
import com.doyens.entity.Recruiter;
import com.doyens.service.LevelService;
import com.doyens.service.RecruiterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/recruiter")
@Api(tags = "招生老师管理接口")
public class RecruiterController {

    @Autowired
    private RecruiterService recruiterService;

    @Autowired
    private LevelService levelService;

    @GetMapping("/page")
    @ApiOperation(value = "招生老师分页查询方法")
    public R<Page> page(int page, int pageSize,String name) {
        //1.构造分页条件对象
        Page<Recruiter> pageInfo = new Page<>(page, pageSize);
        Page<RecruiterDTO> recruiterDTOPage = new Page<>(page, pageSize);
        //2.构建查询及排序条件
        LambdaQueryWrapper<Recruiter> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!=null,Recruiter::getRecruiterName,name);
        wrapper.orderByAsc(Recruiter::getRecruiterId);
        //3.执行分页条件查询
        recruiterService.page(pageInfo, wrapper);
        //4.遍历分页查询列表数据，根据分类ID查询分类信息，从而获取该菜品的分类名称
        //5.拷贝数据，将pageInfo中的数据拷贝到dishDtos中，忽略records字段的数据
        BeanUtils.copyProperties(pageInfo, recruiterDTOPage, "records");
        List<Recruiter> records = pageInfo.getRecords();
        List<RecruiterDTO> list = records.stream().map((item) -> {
            RecruiterDTO recruiterDTO = new RecruiterDTO();
            BeanUtils.copyProperties(item, recruiterDTO);
            Integer levelId = item.getLevelId();
            Level level = levelService.getById(levelId);
            //判断对象是否存在
            if (level != null) {
                String levelName = level.getLevelName();
                recruiterDTO.setLevelName(levelName);
            }
            return recruiterDTO;
        }).collect(Collectors.toList());
        recruiterDTOPage.setRecords(list);
        //5.封装数据并返回
        return R.success(recruiterDTOPage);
    }


    @PostMapping
    @ApiOperation(value = "保存招生老师方法")
    public R<String> save(Recruiter recruiter) {
        //接受的添加菜品的所有字段
        log.info(recruiter.toString());
        boolean save = recruiterService.save(recruiter);
        if (save){
            return R.success("添加招生老师成功...");
        }
        return R.error("添加招生老师失败");
    }

    @DeleteMapping
    @ApiOperation(value = "删除招生老师方法")
    public R<String> delete(Integer id) {
        log.info("删除的招生老师id:{}", id);
        //执行删除
        boolean remove = recruiterService.removeById(id);
        if (remove){
            return R.success("删除招生老师成功");
        }
        return R.error("删除招生老师失败...");
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id回显招生老师信息方法")
    public R<RecruiterDTO> getOne(@PathVariable Integer id) {
        log.info("招生老师id：{}", id);
        RecruiterDTO recruiterDTO = new RecruiterDTO();
        Recruiter recruiter = recruiterService.getById(id);
        BeanUtils.copyProperties(recruiter, recruiterDTO);
        //获取招生老师对应级别id
        Integer levelId = recruiter.getLevelId();
        Level level = levelService.getById(levelId);
        if (level != null) {
            String levelName = level.getLevelName();
            recruiterDTO.setLevelName(levelName);
        }
        return R.success(recruiterDTO);
    }

    @PutMapping
    @ApiOperation(value = "修改招生老师信息方法")
    public R<String> update(RecruiterDTO recruiterDTO){
        log.info("修改后的招生老师信息:{}",recruiterDTO.toString());
        boolean update = recruiterService.updateById(recruiterDTO);
        if (update){
            return R.success("修改招生老师信息成功...");
        }
        return R.error("修改招生老师信息失败...");
    }

    @GetMapping("/list")
    @ApiOperation(value = "下拉框展示招生老师列表方法")
    public R<List<Recruiter>> list(){
        List<Recruiter> list = recruiterService.list();
        return R.success(list);
    }
}
